package com.qiguliuxing.dts.db.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiguliuxing.dts.db.dao.DtsFeedbackMapper;
import com.qiguliuxing.dts.db.domain.DtsFeedback;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author qiguliuxing
 * @date 2017/12/27
 */
@Service
public class DtsFeedbackService {
	@Autowired
	private DtsFeedbackMapper feedbackMapper;

	public Integer add(DtsFeedback feedback) {
		feedback.setAddTime(LocalDateTime.now());
		feedback.setUpdateTime(LocalDateTime.now());
		return feedbackMapper.insert(feedback);
	}

	public List<DtsFeedback> querySelective(Integer userId, String username, Integer page, Integer limit, String sort,
			String order) {
		QueryWrapper<DtsFeedback> queryWrapper = new QueryWrapper<>();
		if (userId != null) {
			queryWrapper.lambda().eq(DtsFeedback::getUserId, userId);
		}
		if (StrUtil.isNotEmpty(username)) {
			queryWrapper.eq("username", username);
		}
		queryWrapper.eq("deleted", false);

		if (StrUtil.isNotEmpty(sort) && StrUtil.isNotEmpty(order)) {
			queryWrapper.orderByDesc(sort);
		}
		IPage<DtsFeedback> iPage = new Page<>(page, limit);
		return feedbackMapper.selectPage(iPage, queryWrapper).getRecords();
	}
}
